package com.gxar.uaa.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.common.entity.login.LoginUserThread;
import com.gxar.common.entity.login.UserProfile;
import com.gxar.uaa.server.base.model.role.vo.request.DeleteRoleUserRequest;
import com.gxar.uaa.server.base.model.role.vo.request.RoleCreateRequest;
import com.gxar.uaa.server.base.model.role.vo.request.RoleToUserRequest;
import com.gxar.uaa.server.base.model.role.vo.request.RoleUpdateRequest;
import com.gxar.uaa.server.base.model.role.vo.response.RoleCreateResponse;
import com.gxar.uaa.server.base.model.role.vo.response.RoleInfoResponse;
import com.gxar.uaa.server.base.model.role.vo.response.RoleUpdateResponse;
import com.gxar.uaa.server.base.model.role.vo.response.RoleUserResponse;
import com.gxar.uaa.server.db.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/1/24 17:51
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/admin/api/roles")
@RequiredArgsConstructor
@Slf4j
public class RoleController {

    private final RoleService roleService;

    @ApiOperation("分页查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
    })
    @GetMapping("")
    public Result<PageResult<RoleInfoResponse>> pageList(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                         @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {
        return roleService.pageList(pageNum, pageSize);
    }

    @ApiOperation("后台管理查询所有角色")
    @GetMapping("/all")
    public Result<List<RoleInfoResponse>> findAll() {
        List<RoleInfoResponse> result = roleService.findAll();
        return Result.success(result);
    }

    @ApiOperation("创建角色")
    @PostMapping()
    public Result<RoleCreateResponse> create(@RequestBody @Valid RoleCreateRequest request) {
        RoleCreateResponse response = roleService.create(request);
        return Result.success(response);
    }

    @ApiOperation("更新角色")
    @PutMapping("/{id}")
    public Result<RoleUpdateResponse> update(@PathVariable Long id, @RequestBody @Valid RoleUpdateRequest request) {
        RoleUpdateResponse response = roleService.update(id, request);
        return Result.success(response);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        roleService.delete(id);
        return Result.success("操作成功", true);
    }

    @ApiOperation("用户分配角色")
    @PostMapping("/granted")
    public Result<Boolean> setRoleToUser(@RequestBody @Valid RoleToUserRequest sysMenu) {
        UserProfile userProfile = LoginUserThread.get();
        roleService.setRoleToUser(userProfile, sysMenu.getUserId(), sysMenu.getRoleIds());
        return Result.success("操作成功", true);
    }

    @ApiOperation("删除角色的用户")
    @DeleteMapping("/user")
    public Result<Boolean> deleteRoleUser(@RequestBody @Valid DeleteRoleUserRequest request) {
        UserProfile userProfile = LoginUserThread.get();
        roleService.deleteRoleUser(userProfile, request.getUserId(), request.getRoleIds());
        return Result.success("操作成功", true);
    }

    @ApiOperation("查询角色用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "number"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "number"),
            @ApiImplicitParam(name = "id", value = "角色ID", dataType = "number"),
    })
    @GetMapping("/{id}/users")
    public Result<PageResult<RoleUserResponse>> roleUsers(@RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                          @RequestParam(name = "page_size", defaultValue = "10") int pageSize,
                                                          @PathVariable("id") Long id) {
        UserProfile userProfile = LoginUserThread.get();
        return roleService.roleUsers(userProfile, id, pageNum, pageSize);
    }

}
